<?php
/*
 * @Author: your name
 * @Date: 2020-10-22 10:57:14
 * @LastEditTime: 2020-12-16 13:49:23
 * @LastEditors: Please set LastEditors
 * @Description: netfilter模块登录
 * @FilePath: /special/application/api/service/client_login/NetfilterLoginService.php
 */
namespace app\api\service\client_login;

use app\common\definition\HostDefinition;
use app\common\definition\netfilter\FirewallRuleDefinition;
use app\common\exception\CustomException;
use app\common\service\HostLink;
use app\common\service\module_manage\netfilter\HostLearnConfig;
use app\common\service\ServerProxy;
use think\Db;

class NetfilterLoginService extends LoginService
{
    public $module = 'netfilter';
    /**
     * @name: netfilter 服务
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-12-07 14:47:03
     */
    public function login ()
    {
        $HostLearnConfig = new HostLearnConfig();
        $netfilterConfig = $HostLearnConfig->findOne($this->host_id);
        if(empty($netfilterConfig))
        {
            throw new CustomException('未找到netfilter配置', 422);
        }
        $last_firewall_rule_time = Db::name('netfilter_firewall_rule_log')->where('host_id', $this->host_id)->whereIn('type', [FirewallRuleDefinition::INIT_RULE_GROUP,FirewallRuleDefinition::NORMAL_RULE_GROUP,FirewallRuleDefinition::RESET_RULE_GROUP] )->order('id', 'DESC')->value('create_time');
        $return['LastFirewallRuleTime'] = empty($last_firewall_rule_time) ? 0 : $last_firewall_rule_time;
        $controlled_remain_time = $netfilterConfig['controlled_firewall'] - time();
        $return['ControlledFirewall'] = $netfilterConfig['controlled_firewall'] ? true : false;
        $return['ControlledFirewallRemainTime'] = $controlled_remain_time > 0 ? $controlled_remain_time : 0 ;
        if($this->host_os == HostDefinition::LINUX_OPERATION_SYSTEM)
        {
            $firewall_set = Db::name('netfilter_firewall_config_linux')->field('main_switch,input,output,forward')->where('host_id', $this->host_id)->find();

            $firewallStatus = array_map(function($v){
                return $v ? true : false;
            }, $firewall_set);

            $return['FirewallConfig'] = [
                'Enable' => $firewallStatus['main_switch'],
                'In' => $firewallStatus['input'],
                'Out' => $firewallStatus['output'],
                'Forward' => $firewallStatus['forward'],
            ];
        }
        else
        {
            $firewall_set = Db::name('netfilter_firewall_config_windows')->field('private_main_switch,private_input,private_output,public_main_switch,public_input,public_output,domain_main_switch,domain_input,domain_output')->where('host_id', $this->host_id)->find();
            $firewallStatus = array_map(function($v){
                return $v ? true : false;
            }, $firewall_set);

            $return['FirewallConfig'] = [
                //私有
                'PrivateEnable' => $firewallStatus['private_main_switch'],
                'PrivateIn' => $firewallStatus['private_input'],
                'PrivateOut' => $firewallStatus['private_output'],
                //公共
                'PublicEnable' => $firewallStatus['public_main_switch'],
                'PublicIn' => $firewallStatus['public_input'],
                'PublicOut' => $firewallStatus['public_output'],
                //域
                'DomainEnable' => $firewallStatus['domain_main_switch'],
                'DomainIn' => $firewallStatus['domain_input'],
                'DomainOut' => $firewallStatus['domain_output'],
            ];
        }

        $HostLearnConfig->autoStartLearn($this->host_id);

        $return['FirewallLogStatus'] = ['Enable' => $netfilterConfig['log_status'] ? true : false];

        $keep_time =$netfilterConfig['etime'] - time();
        $return['Study'] = [
            'Time' =>  $keep_time > 0 ? $keep_time : 0,
            'Mark' => $netfilterConfig['have_mac_addr'] ? true :false,
            'LearnNumber' => $netfilterConfig['learn_number'],
        ];
        $serverProxy = new ServerProxy();
        $return['filter'] = $serverProxy->findProxyData();

        return $return;
    }
}